package com.prs.crm.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.sys.Menu;
import com.prs.crm.domain.sys.ShortCutMenu;
import com.prs.crm.service.sys.MenuService;
import com.prs.crm.service.sys.ShortCutMenuService;

public class ShortCutMenuAction  extends BaseAction{

	private static final long serialVersionUID = 1L;

	private MenuService menuService;
	
	private ShortCutMenuService shortCutMenuService;
	
	private Collection<Menu> menus;
	
	private Map<String,ShortCutMenu> shortCutMenus;
	
	private String menuIds;

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}

	public String getMenuIds() {
		return menuIds;
	}

	public void setShortCutMenus(Map<String,ShortCutMenu> shortCutMenus) {
		this.shortCutMenus = shortCutMenus;
	}

	public Map<String,ShortCutMenu> getShortCutMenus() {
		return shortCutMenus;
	}

	public void setShortCutMenuService(ShortCutMenuService shortCutMenuService) {
		this.shortCutMenuService = shortCutMenuService;
	}

	public ShortCutMenuService getShortCutMenuService() {
		return shortCutMenuService;
	}

	public void setMenus(Collection<Menu> menus) {
		this.menus = menus;
	}

	public Collection<Menu> getMenus() {
		return menus;
	}
	
	@Action("shortCutMenu")
	public String execute()
	{
		Collection<ShortCutMenu> smenus = this.getShortCutMenuService().findMenus(AppContext.getAccountContext().currentUser());
		if (smenus!=null && smenus.size()>0)
		{
			menus = new ArrayList<Menu>();
			for (ShortCutMenu smenu : smenus)
			{
				menus.add(smenu.getMenu());
			}
		}
		else
		{
			menus = this.getMenuService().getShortCutMenus();
		}
		return SUCCESS;
	}
	
	@Action("editShortCutMenu")
	public String editShortCutMenu()
	{
		menus = this.getMenuService().getShortCutMenus();
		Collection<ShortCutMenu> smenus = this.getShortCutMenuService().findMenus(AppContext.getAccountContext().currentUser());

		shortCutMenus = new HashMap<String,ShortCutMenu>();
		for (ShortCutMenu smenu : smenus)
		{
			shortCutMenus.put(smenu.getMenu().getId().toString(), smenu);
		}
		return SUCCESS;
	}
	
	@Action(value="saveShortCutMenu",results={@Result(name="success",type="json",params={"root","prompt"})})
	public String saveShortCutMenu()
	{
		try
		{
			this.getShortCutMenuService().saveShortCutMenus(AppContext.getAccountContext().currentUser()
								, ToolKits.convertStringAryToIntAry(menuIds, ","));
			this.setPrompt("success");
			return SUCCESS;
		
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return ERROR;
		}
	}
	
}
